package com.yfuxdeer.youchat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.pojo.User;
import com.yfuxdeer.youchat.service.UserService;
import com.yfuxdeer.youchat.utils.StorageUtil;
import com.yfuxdeer.youchat.utils.Store;

import java.util.Timer;
import java.util.TimerTask;

public class LaunchActivity extends AppCompatActivity {

    private Store store = Store.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        //注册广播
        registerReceiver(loginReceiver, new IntentFilter("com.yfuxdeer.youchat.loginReceiver"));

        // 使用定时器延迟一毫秒跳转，直接使用Thread.sleep()会显示不出页面
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                User user = StorageUtil.readUser(getApplicationContext());
                if (user != null) {
                    //已经记住过，启动服务验证一下验证
                    Intent intent = new Intent(getApplicationContext(), UserService.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username", user.getUsername());
                    bundle.putString("password", user.getPassword());
                    bundle.putSerializable("method", UserService.METHOD.LOGIN);
                    intent.putExtras(bundle);
                    startService(intent);
                } else {
                    //没有登陆过，跳转登录
                    startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                    finish();
                }
            }
        };
        timer.schedule(timerTask, 300);
    }

    private BroadcastReceiver loginReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            UserService.RESULT result = (UserService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    //验证通过
                    User user = (User) bundle.getSerializable("loginUser");
                    store.setCurrentUser(user);
                    startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                    finish();
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(LaunchActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    break;
                case ERROR:
                    //验证失败
                    startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                    finish();
                    break;
            }
        }
    };
}